<%@ page import="com.xpmusic.model.MusicInfo" %>
<%@ page import="java.util.List" %>
<%@ page import="com.xpmusic.dao.impl.MusicDaoImpl" %><%--
  Created by IntelliJ IDEA.
  User: 纯牛奶丶
  Date: 2017/12/27
  Time: 15:36
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>管理员管理歌曲信息</title>
    <link rel="stylesheet" href="./css/musicList.css">
    <%
        String url = request.getContextPath() + "/deleteMusicServlet?musicName=";
        String  illegalFile = (String) session.getAttribute("illegalFile");
        String  deleteSuccess = (String) session.getAttribute("deleteSuccess");
    %>
    <script>
        <%if(illegalFile != null){%>
            alert("${illegalFile}");
        <%
             session.removeAttribute("illegalFile");
        }%>
        <%if(deleteSuccess != null){%>
        alert("${deleteSuccess}");
        <%
            session.removeAttribute("deleteSuccess");
        }%>
    </script>
    <script>
        function getXMLHttpRequest() {
            var xmlhttp;
            if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
                xmlhttp = new XMLHttpRequest();
            }
            else {// code for IE6, IE5
                xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
            }
            return xmlhttp;
        }
        //更新数据库
        function update() {
            var req1=getXMLHttpRequest();
            req1.onreadystatechange=function () {
                if(req1.readyState===4){
                    // alert(req.status);//查看服务器端响应状态
                    if(req1.status===200){
                        var text = eval("("+req1.responseText+")");
                        if(text.result==="true"){
                            window.location.href="musicList.jsp";
                        }else{
                            window.location.href="musicList.jsp";
                        }
                    }
                }
            };
            //2.建立一个连接
            req1.open("POST","UpdateDB");
            //设置请求头
            req1.setRequestHeader("Content-type","application/json");
            //3.发送请求
            req1.send();
        }
    </script>
</head>
<body>
<div id="uploadMusic">
    <h3>管理员上传文件</h3>
    <form action="${pageContext.request.contextPath}/uploadMusicServlet" enctype="multipart/form-data" method="post">
        <p>请选择文件</p>
        <input type="file" name="fileUpload" id="fileUpload" multiple="multiple" accept="audio/mpeg"><br>
        <input type="submit" value="上传"/>&nbsp;&nbsp;<input type="button" onclick="update()" value="更新数据库"><br/>
    </form>
</div>
<div id="music_list" width="500px">
    <table border="1">
        <tr>
            <th>歌名</th>
            <th>歌手</th>
            <th>歌曲信息</th>
            <th>删除</th>
        </tr>
        <%
            List<MusicInfo> musicInfos = new MusicDaoImpl().getAllMusic();
            for(MusicInfo musicInfo : musicInfos){
        %>
            <tr>
                <td><%=musicInfo.getMusicName()%></td>
                <td><%=musicInfo.getSinger()%></td>
                <td><%=musicInfo.getMusicInfo()%></td>
                <td><a href="<%=url+musicInfo.getMusicPath()%>">删除</a></td>
            </tr>
        <%
            }
        %>
    </table>
</div>

</body>
</html>
